//
//  TSUserInfoViewController.m
//  chart
//
//  Created by xu on 15/8/22.
//  Copyright (c) 2015年 xu. All rights reserved.
//

#import "TSUserInfoViewController.h"
#import "TSUserInfoEditViewController.h"
#import "TSPasswordEditViewController.h"

#define SEARCH_LABEL_WIDTH  50.f
#define SEARCH_LABEL_HEIGHT 35.f
#define SEARCH_ROW_GAP 10.f

@interface TSUserInfoViewController ()

@end

@implementation TSUserInfoViewController{
    UILabel *titleLabel;
    UILabel *workIdLabel;
    UILabel *sectionLabel;
    UILabel *phoneLabel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshUserInfo:) name:TSNotification_EditUserInfoSucceeded object:nil];
    
    [self setTitle:@"个人信息"];
    
    [self addTitleRow];
    
    [self addWorkIdRow];
    
    [self addSctionRow];
    
    [self addPhoneRow];
    
    [self addEditInfoBtn];
    
    [self addEditPwdBtn];
    
    [self addLogoutBtn];
}

- (void)refreshUserInfo:(NSNotification *)noti
{
    titleLabel.text = [TSUserModel sharedInstance].name;
    workIdLabel.text = [TSUserModel sharedInstance].workId;
    phoneLabel.text = [TSUserModel sharedInstance].phone;
}

- (UILabel *)createLabel:(CGRect)frame Title:(NSString *)title
{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    [label setText:title];
    [label setFont:FONT(12.f)];
    
    return label;
}

- (void)addLine:(CGRect)frame
{
    UIView *line = [[UIView alloc] initWithFrame:frame];
    [line setBackgroundColor:[UIColor lightGrayColor]];
    [self.view addSubview:line];
}

- (void)addTitleRow
{
    UILabel *label = [self createLabel:CGRectMake(MARGIN_LEFT, MARGIN_TOP, SEARCH_LABEL_WIDTH, SEARCH_LABEL_HEIGHT) Title:@"姓名:"];
    [self.view addSubview:label];
    
    titleLabel = [self createLabel:CGRectMake(CGRectGetMaxX(label.frame)+10.f, CGRectGetMinY(label.frame), currentDeviceWidth-2*MARGIN_LEFT-SEARCH_LABEL_WIDTH-10.f, SEARCH_LABEL_HEIGHT) Title:[TSUserModel sharedInstance].name];
    [self.view addSubview:titleLabel];
    [self addLine:CGRectMake(CGRectGetMinX(titleLabel.frame), CGRectGetMaxY(titleLabel.frame)-1.f, CGRectGetWidth(titleLabel.frame), .5f)];
}

- (void)addWorkIdRow
{
    UILabel *label = [self createLabel:CGRectMake(MARGIN_LEFT, CGRectGetMaxY(titleLabel.frame)+SEARCH_ROW_GAP, SEARCH_LABEL_WIDTH, SEARCH_LABEL_HEIGHT) Title:@"工号:"];
    [self.view addSubview:label];
    
    workIdLabel = [self createLabel:CGRectMake(CGRectGetMaxX(label.frame)+10.f, CGRectGetMinY(label.frame), CGRectGetWidth(titleLabel.frame), SEARCH_LABEL_HEIGHT) Title:[TSUserModel sharedInstance].workId];
    [self.view addSubview:workIdLabel];
    [self addLine:CGRectMake(CGRectGetMinX(workIdLabel.frame), CGRectGetMaxY(workIdLabel.frame)-1.f, CGRectGetWidth(workIdLabel.frame), .5f)];
}

- (void)addSctionRow
{
    UILabel *label = [self createLabel:CGRectMake(MARGIN_LEFT, CGRectGetMaxY(workIdLabel.frame)+SEARCH_ROW_GAP, SEARCH_LABEL_WIDTH, SEARCH_LABEL_HEIGHT) Title:@"部门:"];
    [self.view addSubview:label];
    
    sectionLabel = [self createLabel:CGRectMake(CGRectGetMaxX(label.frame)+10.f, CGRectGetMinY(label.frame), CGRectGetWidth(workIdLabel.frame), SEARCH_LABEL_HEIGHT) Title:[TSUserModel sharedInstance].section];
    [self.view addSubview:sectionLabel];
    [self addLine:CGRectMake(CGRectGetMinX(sectionLabel.frame), CGRectGetMaxY(sectionLabel.frame)-1.f, CGRectGetWidth(sectionLabel.frame), .5f)];
}

- (void)addPhoneRow
{
    UILabel *label = [self createLabel:CGRectMake(MARGIN_LEFT, CGRectGetMaxY(sectionLabel.frame)+SEARCH_ROW_GAP, SEARCH_LABEL_WIDTH, SEARCH_LABEL_HEIGHT) Title:@"电话:"];
    [self.view addSubview:label];
    
    phoneLabel = [self createLabel:CGRectMake(CGRectGetMaxX(label.frame)+10.f, CGRectGetMinY(label.frame), CGRectGetWidth(sectionLabel.frame), SEARCH_LABEL_HEIGHT) Title:[TSUserModel sharedInstance].phone];
    [self.view addSubview:phoneLabel];
    [self addLine:CGRectMake(CGRectGetMinX(phoneLabel.frame), CGRectGetMaxY(phoneLabel.frame)-1.f, CGRectGetWidth(phoneLabel.frame), .5f)];
}

/* 修改个人信息 */
- (void)addEditInfoBtn
{
    UIButton *btn = [self createBtn:CGRectMake(MARGIN_LEFT, CGRectGetMaxY(phoneLabel.frame)+SEARCH_ROW_GAP, currentDeviceWidth-2*MARGIN_LEFT, SEARCH_LABEL_HEIGHT) Title:@"修改信息"];
    [self.view addSubview:btn];
}

/* 修改密码 */
- (void)addEditPwdBtn
{
    UIButton *btn = [self createBtn:CGRectMake(MARGIN_LEFT, CGRectGetMaxY(phoneLabel.frame)+2*SEARCH_ROW_GAP+SEARCH_LABEL_HEIGHT, currentDeviceWidth-2*MARGIN_LEFT, SEARCH_LABEL_HEIGHT) Title:@"修改密码"];
    [self.view addSubview:btn];
}

/* 退出登录 */
- (void)addLogoutBtn
{
    UIButton *btn = [self createBtn:CGRectMake(MARGIN_LEFT, CGRectGetMaxY(phoneLabel.frame)+3*SEARCH_ROW_GAP+2*SEARCH_LABEL_HEIGHT, currentDeviceWidth-2*MARGIN_LEFT, SEARCH_LABEL_HEIGHT) Title:@"退出登录"];
    [self.view addSubview:btn];
}

- (UIButton *)createBtn:(CGRect)frame Title:(NSString *)title
{
    UIButton *btn = [[UIButton alloc] initWithFrame:frame];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(onBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    btn.titleLabel.font = FONT(12);
    [btn setBackgroundColor:[UIColor grayColor]];
    [btn.layer setCornerRadius:5.f];
    
    return btn;
}

- (void)onBtnClick:(UIButton *)sender
{
    if ([sender.titleLabel.text isEqualToString:@"修改信息"]) {
        [self showEditInfoViewController];
    }
    else if ([sender.titleLabel.text isEqualToString:@"修改密码"]){
        [self showEditPwdViewController];
    }
    else if ([sender.titleLabel.text isEqualToString:@"退出登录"]){
        UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"退出登录" otherButtonTitles:nil, nil];
        [sheet showInView:self.view];
    }
}

- (void)showEditInfoViewController
{
    TSUserInfoEditViewController *edit_vc = [[TSUserInfoEditViewController alloc] init];
    [self.navigationController pushViewController:edit_vc animated:YES];
}

- (void)showEditPwdViewController
{
    TSPasswordEditViewController *edit_vc = [[TSPasswordEditViewController alloc] init];
    [self.navigationController pushViewController:edit_vc animated:YES];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        WS(weakSelf);
        [[TSUserModel sharedInstance] logout:^(BOOL isSucceeded, NSError *error) {
            if (!weakSelf) {
                return;
            }
            
            if (isSucceeded) {
                [weakSelf.navigationController popToRootViewControllerAnimated:YES];
            }
        }];
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


@end
